package com.ls.pattern.proxy.myproxy.client;

import com.ls.pattern.proxy.myproxy.MyClassLoader;
import com.ls.pattern.proxy.myproxy.MyInvocationHanlder;
import com.ls.pattern.proxy.myproxy.MyProxy;

import java.lang.reflect.Method;

/**
 * 点餐员
 *
 * @author 挥之以墨
 */
public class MyWaiter implements MyInvocationHanlder {

    private Object target;

    public Object getInstance(Object target) {
        this.target = target;
        return MyProxy.newProxyInstance(new MyClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(this.target, args);
        after();
        return invoke;
    }

    private void before() {
        System.out.println("这里是菜单，需要点什么？");
    }

    private void after() {
        System.out.println("好的，已通知后厨，菜马上就好。");
    }
}
